/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月19日 下午11:56:48
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.h5.controller.order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.IPayApplyConnector;
import com.itttq.gshop.generic.controller.GenericController;

/**   
 * @ClassName:  PayController
 * @author: 迷蝶
 * @date:   2018年1月19日 下午11:56:48 
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
@Auth
@RestController
@RequestMapping("pay")
public class PayController extends GenericController {
	private static final long serialVersionUID = -5917602719765774816L;
	private static Logger LOG = LoggerFactory.getLogger(PayController.class);
	@Autowired
	private IPayApplyConnector payApplyConnector;
	
	/**
	 * 根据订单ID生成支付请求<br>
	 * 如果是线下支付，直接为支付成功并更新订单状态。<br>
	 * 线上支付则跳转到第三方
	 * 
	 * @param orderId		订单ID
	 * @param payMethodId		支付方式ID。写死id=1为线下支付
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("apply.html")
	public ModelAndView payApply(int orderId,  int payMethodId) {
		LOG.info("pay apply： orderId = {}, payMethodId = {}", orderId, payMethodId);
		ModelAndView mv = new ModelAndView("/order/payment");
		ResultTo rt = payApplyConnector.payApply(orderId, payMethodId);
		LOG.info("pay apply add result:  {}", rt);
		mv.addObject("code", rt.getCode());
		mv.addObject("msg", rt.getMsg());
		if(rt.getCode() == ResultTo.SUCCESS) {
			if(payMethodId == 1) {
				// 指定1为线下支付ID
				mv.setViewName("/order/pay-result"); // 跳转到支付成功页面
				mv.addObject("msg", "支付成功");
			} else if(payMethodId == 3) {
				// 微信支付，跳转到微信支付页面发起公众号支付。此时data为微信支付返回的相关信息JSON格式
				mv.setViewName("/order/pay-wx");
				mv.addObject("data", rt.getMsg());
			}
		}
		return mv;
	}
	@RequestMapping("success.html")
	public ModelAndView paySuccess() {
		ModelAndView mv = new ModelAndView("/order/pay-result");
		mv.addObject("msg", "支付成功");
		return mv;
	}
	@RequestMapping("fail.html")
	public ModelAndView payFail() {
		ModelAndView mv = new ModelAndView("/order/pay-result");
		mv.addObject("msg", "支付遇到问题");
		return mv;
	}
    
}
